import scrapy
import re
import json
from bili_spider.items import BiliSpiderItem
class NewsqqSpider(scrapy.Spider):
    name = "spider"

    start_urls =[
        "https://api.bilibili.com/pgc/season/index/result?season_version=-1&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&order=3&st=1&sort=0&page={}&season_type=1&pagesize=50&type=1".format(i) for i in range(1,63)
    ]
    def parse(self, response):
        rs = json.loads(response.text)

        for i in rs["data"]["list"]:
            item = BiliSpiderItem()
            item["url"]= i["link"]
            item["anime_name"] = i["title"]
            item["media_id"] = str(i["media_id"])
            item["order"] = i["order"]
            url ="https://www.bilibili.com/bangumi/media/md"+str(item["media_id"])
            request = scrapy.Request(url=url,
                                     callback=self.play_parse,
                                     meta={"item": item})
            yield request
    def play_parse(self,response):
        item = response.meta['item']
        item["media_info_count_item_play"] = response.xpath("//span[contains(@class,'media-info-count-item-play')]/em/text()").extract_first()
        item["media_info_count_item_fans"] = response.xpath("//span[contains(@class,'media-info-count-item-fans')]/em/text()").extract_first()
        item["media_info_count_item_review"] = response.xpath("//span[contains(@class,'media-info-count-item-review')]/em/text()").extract_first()
        if len(response.xpath('//ul[@class="clearfix"]/li/text()').extract()) ==3:
            item["short_comment"] = response.xpath('//ul[@class="clearfix"]/li/text()').extract()[1]
            item["long_comment"] = response.xpath('//ul[@class="clearfix"]/li/text()').extract()[2]
        else:
            item["short_comment"]= "null"
            item["long_comment"] = "null"
        item["score"] =  response.xpath('//div[@class="media-info-score-content"]/text()').extract_first()
        item['score_all_people'] = response.xpath('//div[@class="media-info-review-times"]/text()').extract_first()
        item["media_tags"] = response.xpath('//span[@class="media-tags"]/span/text()').extract()
        yield item


